package com.innovations.inn.controller;

import com.innovations.common.annotation.Log;
import com.innovations.common.core.controller.BaseController;
import com.innovations.common.core.domain.AjaxResult;
import com.innovations.common.core.page.TableDataInfo;
import com.innovations.common.enums.BusinessType;
import com.innovations.common.utils.poi.ExcelUtil;
import com.innovations.framework.web.service.TokenService;
import com.innovations.inn.domain.BusinessIndividualModify;
import com.innovations.inn.domain.ResearchInstitution;
import com.innovations.inn.domain.response.ResponseBusinessIndividual;
import com.innovations.inn.domain.response.ResponseItemCount;
import com.innovations.inn.service.IBusinessIndividualModifyService;
import com.innovations.inn.service.IResearchInstitutionService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 商业用户信息修改记录Controller
 *
 * @author dp
 * @date 2024-11-08
 */
@Api(tags = "商业用户信息修改记录")
@RestController
@RequestMapping("/inn/business/individual/modify")
public class BusinessIndividualModifyController extends BaseController {
    @Autowired
    TokenService tokenService;
    @Autowired
    private IBusinessIndividualModifyService businessIndividualModifyService;
    @Autowired
    IResearchInstitutionService researchInstitutionService;

    /**
     * 查询商业用户信息修改记录列表
     */
    @ApiOperation("查询商业用户信息修改记录列表")
    @PreAuthorize("@ss.hasPermi('inn:modify:list')")
    @GetMapping("/list")
    public TableDataInfo list(BusinessIndividualModify businessIndividualModify) {
        startPage();
        List<BusinessIndividualModify> list = businessIndividualModifyService.selectBusinessIndividualModifyList(businessIndividualModify);
        return getDataTable(list);
    }

    /**
     * 查询用户信息待审核记录列表
     */
    @ApiOperation("查询商业用户信息修改记录列表")
    @PreAuthorize("@ss.hasPermi('inn:modify:list')")
    @GetMapping("/user/list")
    public TableDataInfo userList(Long userId, Long status) {
        ResearchInstitution researchOutcomes = new ResearchInstitution();
        researchOutcomes.setUserId(userId);
        List<ResearchInstitution> list = researchInstitutionService.selectResearchInstitutionList(researchOutcomes);
        if (list == null || list.isEmpty()) {
            throw new RuntimeException("当前用户无可管理机构");
        }
        BusinessIndividualModify roInput = new BusinessIndividualModify();

        roInput.setInstitutionId(list.get(0).getInstitutionId());

        if (status != null) {
            roInput.setState(status);
        }

        startPage();
        List<ResponseBusinessIndividual> Returnlist = businessIndividualModifyService.selectBusinessIndividualModifyListInfo(roInput);
        return getDataTable(Returnlist);
    }

    /**
     * 查询商业用户扩展信息修改记录列表
     */
    @ApiOperation("查询商业用户扩展信息修改记录列表")
    @PreAuthorize("@ss.hasPermi('inn:modify:list')")
    @GetMapping("/list/info")
    public TableDataInfo listInfo(BusinessIndividualModify businessIndividualModify) {
        startPage();
        List<ResponseBusinessIndividual> list = businessIndividualModifyService.selectBusinessIndividualModifyListInfo(businessIndividualModify);
        return getDataTable(list);
    }

    /**
     * 查询商业用户扩展信息修改记录合计
     */
    @ApiOperation("查询商业用户扩展信息修改记录合计")
    @PreAuthorize("@ss.hasPermi('inn:modify:list')")
    @GetMapping("/list/info/count")
    public TableDataInfo listInfoCount(BusinessIndividualModify businessIndividualModify) {
        startPage();
        List<ResponseItemCount> listCount = businessIndividualModifyService.selectBusinessIndividualModifyListInfoCount(businessIndividualModify);
        return getDataTable(listCount);
    }

    /**
     * 导出商业用户信息修改记录列表
     */
    @PreAuthorize("@ss.hasPermi('inn:modify:export')")
    @Log(title = "商业用户信息修改记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, BusinessIndividualModify businessIndividualModify) {
        List<BusinessIndividualModify> list = businessIndividualModifyService.selectBusinessIndividualModifyList(businessIndividualModify);
        ExcelUtil<BusinessIndividualModify> util = new ExcelUtil<BusinessIndividualModify>(BusinessIndividualModify.class);
        util.exportExcel(response, list, "商业用户信息修改记录数据");
    }

    /**
     * 获取商业用户信息修改记录详细信息
     */
    @ApiOperation("获取商业用户信息修改记录详细信息")
    @PreAuthorize("@ss.hasPermi('inn:modify:query')")
    @GetMapping(value = "/{businessIndividualId}")
    public AjaxResult getInfo(@PathVariable("businessIndividualId") Long businessIndividualId) {
        return success(businessIndividualModifyService.selectBusinessIndividualModifyByBusinessIndividualId(businessIndividualId));
    }

    /**
     * 新增商业用户信息修改记录
     */
    @ApiOperation("新增商业用户信息修改记录")
    @PreAuthorize("@ss.hasPermi('inn:modify:add')")
    @Log(title = "商业用户信息修改记录", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(HttpServletRequest request, @RequestBody BusinessIndividualModify businessIndividualModify) {
        Long userId = tokenService.getLoginUser(request).getUser().getUserId();
        businessIndividualModifyService.completeData(businessIndividualModify, userId, 1);
        if (!businessIndividualModifyService.checkBusinessIndividualModifyUnique(businessIndividualModify)) {
            return AjaxResult.error("存在未审核的修改记录");
        }
        ;
        return toAjax(businessIndividualModifyService.insertBusinessIndividualModify(businessIndividualModify));
    }

    /**
     * 修改商业用户信息修改记录
     */
    @ApiOperation("修改商业用户信息修改记录")
    @PreAuthorize("@ss.hasPermi('inn:modify:edit')")
    @Log(title = "商业用户信息修改记录", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(HttpServletRequest request, @RequestBody BusinessIndividualModify businessIndividualModify) {
        Long userId = tokenService.getLoginUser(request).getUser().getUserId();
        businessIndividualModifyService.completeData(businessIndividualModify, userId, 2);
        return toAjax(businessIndividualModifyService.updateBusinessIndividualModify(businessIndividualModify));
    }

    /**
     * 删除商业用户信息修改记录
     */
    @ApiOperation("删除商业用户信息修改记录")
    @PreAuthorize("@ss.hasPermi('inn:modify:remove')")
    @Log(title = "商业用户信息修改记录", businessType = BusinessType.DELETE)
    @DeleteMapping("/{businessIndividualIds}")
    public AjaxResult remove(@PathVariable Long[] businessIndividualIds) {
        return toAjax(businessIndividualModifyService.deleteBusinessIndividualModifyByBusinessIndividualIds(businessIndividualIds));
    }
}
